<?php
/**
 * Geneone: Content Management Framework
 * Request Class
 *
 * http://geneone.neondragon.net/
 *
 * @package Geneone
 * @author Khlo
 * @version 1.0
 * @copyright 2004-2006 Geneone Development Team
 */
 
define ('GENE_REQUEST_GET', 1);
define ('GENE_REQUEST_POST', 2);
define ('GENE_REQUEST_COOKIE', 4);
 
class Gene_Request {
	/**
	 * Tidy an array recursively
	 *
	 * @var string/array $value Value
	 * @return array 
	 */
	function _recursiveClean($value) {
		if (is_array($value)) {
			return array_map(array("Gene_Request", "_recursiveClean"), $value);
		} else {
			return stripslashes($value);
		}
	}
	
	/**
	 * Return HTTP input in any PHP datatype.
	 *
	 * @var string $name Input Name
	 * @var integer $source Input Source. Constants: GENE_REQUEST_GET,
	 *                      GENE_REQUEST_POST, GENE_REQUEST_COOKIE. In order
	 *                      of precedent: POST, COOKIE, GET.
	 * @var boolean $cast Cast data into required type if required. If false,
	 *                    input will not be returned if wrong datatype (not
	 *                    applicable to getAny().
	 */
	function getAny($name, $source, $cast=true) {
		$cookiePrefix = Gene::getSetting("cookie_prefix");
		
		if (($source & GENE_REQUEST_POST) && isset($_POST[$name])) {
			$value = $_POST[$name];
		} else if (($source & GENE_REQUEST_COOKIE) && isset($_COOKIE[$cookiePrefix.$name])) {
			$value = $_COOKIE[$cookiePrefix.$name];
		} else if (($source & GENE_REQUEST_GET) && isset($_GET[$name])) {
			$value = $_GET[$name];
		}
		
		if (isset($value)) {
			if (is_string($value) && get_magic_quotes_gpc()) {
				return stripslashes($value); // Cancel out Magic Quotes
			} else {
				return $value;
			}
		}
	}
	
	/**
	 * Return HTTP input in PHP string.
	 *
	 * @var string $name Input Name
	 * @var integer $source Input Source. Constants: GENE_REQUEST_GET,
	 *                      GENE_REQUEST_POST, GENE_REQUEST_COOKIE. In order
	 *                      of precedent: POST, COOKIE, GET.
	 * @var boolean $cast Cast data into required type if required. If false,
	 *                    input will not be returned if wrong datatype.
	 */
	function getString($name, $source, $cast=true) {
		$value = Gene_Request::getAny($name, $source);
		if ($value !== null) {
			if (is_string($value)) {
				return $value;
			} else {
				if ($cast && is_scalar($value)) {
					return (string)$value;
				}
			}
		}
	}
	
	/**
	 * Return HTTP input in PHP string.
	 *
	 * @var string $name Input Name
	 * @var integer $source Input Source. Constants: GENE_REQUEST_GET,
	 *                      GENE_REQUEST_POST, GENE_REQUEST_COOKIE. In order
	 *                      of precedent: POST, COOKIE, GET.
	 * @var boolean $cast Cast data into required type if required. If false,
	 *                    input will not be returned if wrong datatype.
	 */
	function getInteger($name, $source, $cast=true) {
		$value = Gene_Request::getAny($name, $source);
		if ($value !== null) {
			if (is_numeric($value)) {
				return (int)$value;
			} else {
				if ($cast && is_scalar($value)) {
					return (int)$value;
				}
			}
		}
	}
	
	/**
	 * Return HTTP input in PHP string.
	 *
	 * @var string $name Input Name
	 * @var integer $source Input Source. Constants: GENE_REQUEST_GET,
	 *                      GENE_REQUEST_POST, GENE_REQUEST_COOKIE. In order
	 *                      of precedent: POST, COOKIE, GET.
	 * @var boolean $cast Cast data into required type if required. If false,
	 *                    input will not be returned if wrong datatype. (not
	 *                    applicable to getBoolean().
	 */
	function getBoolean($name, $source, $cast=true) {
		$value = Gene_Request::getAny($name, $source);
		if ($value !== null) {
			if ($value) {
				return true;
			} else {
				return false;
			}
		}
	}
	
	/**
	 * Return HTTP input in PHP string.
	 *
	 * @var string $name Input Name
	 * @var integer $source Input Source. Constants: GENE_REQUEST_GET,
	 *                      GENE_REQUEST_POST, GENE_REQUEST_COOKIE. In order
	 *                      of precedent: POST, COOKIE, GET.
	 * @var boolean $cast Cast data into required type if required. If false,
	 *                    input will not be returned if wrong datatype.
	 */
	function getArray($name, $source, $cast=true) {
		$value = Gene_Request::getAny($name, $source);
		if ($value !== null) {
			if (is_array($value)) {
				if (get_magic_quotes_gpc()) {
					return Gene_Request::_recursiveClean($value);
				}
			} else {
				if ($cast && is_scalar($value)) {
					return array($value);
				}
			}
		}
	}
	
	/**
	 * Return HTTP input in PHP string.
	 *
	 * @var string $name Input Name
	 * @var integer $source Input Source. Constants: GENE_REQUEST_GET,
	 *                      GENE_REQUEST_POST, GENE_REQUEST_COOKIE. In order
	 *                      of precedent: POST, COOKIE, GET.
	 * @var boolean $cast Cast data into required type if required. If false,
	 *                    input will not be returned if wrong datatype.
	 */
	function getFloat($name, $source, $cast=true) {
		$value = Gene_Request::getAny($name, $source);
		if ($value !== null) {
			if (is_numeric($value)) {
				return (float)$value;
			} else {
				if ($cast && is_scalar($value)) {
					return (float)$value;
				}
			}
		}
	}
	
	/**
	 * Return HTTP input if in e-mail address format.
	 *
	 * @var string $name Input Name
	 * @var integer $source Input Source. Constants: GENE_REQUEST_GET,
	 *                      GENE_REQUEST_POST, GENE_REQUEST_COOKIE. In order
	 *                      of precedent: POST, COOKIE, GET.
	 */
	function getEmail($name, $source) {
		require_once 'Validate.php';
		
		$value = Gene_Request::getString($name, $source);
		if (Validate::email($value)) {
			return $value;
		}
	}

	/**
	 * Sets a cookie with prefix, path and domain as configured.
	 *
	 * @var string $name Cookie Name
	 * @var string $value Cookie Value. If blank, cookie will be deleted.
	 * @var integer $timeToLive Time the cookie should live. By default, 360 days.
	 */
	function setCookie($name, $value, $timeToLive=31104000) {
		if ($value == "") {
			$expiry = 1;
		} else {
			if ($timeToLive == "session") {
				$expiry = null;
			}
			
			$expiry = time()+$timeToLive;
		}
		
		setcookie (Gene::getSetting("cookie_prefix").$name,
			$value,
			$expiry,
			Gene::getSetting("cookie_path"),
			Gene::getSetting("cookie_domain")
		);
	}
}
 
?>